/*
 * @Author: Wilbur
 * @Date: 2019-03-21 17:00:00
 * @Last Modified by: Wilbur
 * @Last Modified time: 2019-04-10 16:17:59
 */

<template>
    <div class="container check-list-container">

        <div class="operation-wrapper">
            <el-button
                type="primary"
                @click="handleRoute('vipSet')"
                size="small"
            >会员设置</el-button>
        </div>
        <div class="content-wrapper">
            <!-- <el-row
                class="search-wrapper"
                :gutter="20"
            >
                <el-form
                    label-position="left"
                    label-width="84px"
                    size="small"
                >
                    <el-col
                        class="filter-item"
                        :md="{ span: 16 }"
                        :sm="{ span: 16 }"
                    >
                        <el-input
                            v-model.trim="filterData.search_content"
                            placeholder="请输入广告主登录账号或者公司名称"
                            type="text"
                            size="small"
                            style="width: 250px;"
                            @keyup.native.enter="getTableData()"
                        />
                        <el-button
                            size="small"
                            type="primary"
                            @click="getTableData()"
                        >搜索</el-button>
                    </el-col>
                </el-form>
            </el-row> -->
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                >
                    <el-table-column
                        fixed="left"
                        header-align="left"
                        :width="50"
                        label="序号"
                    >
                        <template slot-scope="scope">
                            <div class="table-index">
                                {{ scope.$index + 1 }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        fixed="left"
                        prop="hire_company_name"
                        show-overflow-tooltip
                        label="企业账号">
                        <template slot-scope="scope">
                            <span v-if="scope.row.hire_company_name == ''">--</span>
                            <a
                                href="javascript:;"
                                v-else
                                style="color: #409EFF;"
                                @click="handleRoute('hireList', {search_content: scope.row.hire_company_name})">{{ scope.row.hire_company_name }}</a>
                        </template>
                    </el-table-column>
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="category"
                        show-overflow-tooltip
                        label="分类"
                    />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="sub_category"
                        show-overflow-tooltip
                        label="具体分类"
                    />
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="content"
                        show-overflow-tooltip
                        label="日志内容"
                    />
                    <el-table-column
                        prop="create_time"
                        :min-width="100"
                        label="生成时间">
                        <template slot-scope="scope">
                            {{ scope.row.create_time*1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                        </template>
                    </el-table-column>


                    <el-table-column
                        prop="remark"
                        :min-width="150"
                        label="备注">
                        <template slot-scope="scope">
                            <template v-if="scope.row.remark">{{ scope.row.remark }}</template>
                            <template v-else>--</template>
                        </template>
                    </el-table-column>

                    <el-table-column
                        prop="date"
                        label="操作"
                        fixed="right"
                        :width="80"
                    >
                        <template slot-scope="scope">
                            <div>
                                <el-button
                                    type="text"
                                    :disabled="scope.row.can_remark == 1 ? false : true"
                                    @click="fnShowRemarkDialog(scope.row.id, scope.row.remark)"
                                >填写备注</el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="page.page_id"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <!-- 填写备注 -->
        <el-dialog
            title="填写备注"
            width="475px"
            @close="resetForm('ruleForm')"
            :visible.sync="showRemarkDialog">
            <el-form
                :model="remarkForm"
                :rules="rules"
                ref="ruleForm"
                label-position="right"
            >
                <el-form-item
                    prop="remark"
                    label-width="0">
                    <el-input
                        type="textarea"
                        rows="5"
                        v-model="remarkForm.remark"
                        autocomplete="off"/>
                </el-form-item>
            </el-form>
            <div
                slot="footer"
                class="dialog-footer">
                <el-button @click="resetForm('ruleForm')">取 消</el-button>
                <el-button
                    type="primary"
                    @click="submitForm('ruleForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {},
    data() {
        return {
            logList: [],
            authResult: {},
            showRemarkDialog: false,
            remarkForm: {
                remark: ''
            },
            rules: {
                remark: [
                    { required: true, message: '请输入备注', trigger: 'blur' }
                ]
            },
            tableData: [],
            filterData: {
                search_content: '',
                vip: 0
            },
            id: '',
            page: {
                page_id: 1,
                total_count: 0,
                page_size: 0
            },
            currTime: 0
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.$store.commit('SET_HEADER', [
            {
                title: "广告系统",
            },
            {
                title: '账单日志'
            }
        ]);
        this.getTableData(this.$route.query.page);
    },
    filters: {
        emptyFilter(val){
            if (!val) {
                return '--';
            }
            return val;
        }
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.fnChangeRemark(this.id);
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.showRemarkDialog = false;
        },
        fnShowRemarkDialog(no, remark) {
            this.id = no;
            this.remarkForm.remark = remark;
            this.showRemarkDialog = true;
        },
        fnChangeRemark(id) {
            this.$post('/wei4/hire_user_log%5Cremark', {
                id: id,
                remark: this.remarkForm.remark,
            }, resp => {
                if (resp.code == 1) {
                    this.resetForm('ruleForm');
                    this.getTableData();
                    this.$message({
                        type: 'success',
                        message: '操作成功',
                        center: true
                    });
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },
        /**
       * 获取表格数据
       */
        getTableData(page) {
            let time = new Date().getTime();

            this.currTime = time;

            this.$router.replace({
                query: {
                    // ...this.filterData,
                    page: page || this.page.page_id
                }
            });
            this.$post('/wei4/hire_user_log%5Cget_list', {
                ...this.filterData,
                page_id: page || this.page.page_id,
            }, resp => {
                if(this.currTime != time){
                    return ;
                }
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.page_id = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                    if (this.page.page_id > resp.data.page.total_page && this.page.page_id !== 1) {
                        this.getTableData(resp.data.page.total_page);
                    }
                } else {
                    this.$message({
                        type: 'warning',
                        message: resp.msg,
                        center: true
                    });
                }
            });
        },
        /**
       * 跳转页面
       */
        handleRoute(name, query) {
            let obj = {
                name
            };

            if (query) {
                obj.query = query;
            }

            this.$router.push(obj);
        },

        /**
       * 列表渲染辅助函数：列表项为空处理
       */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
  .container {
    display: flex;
    flex-direction: column;
    width: 100%;
    .log-title{
    }
  }
  .operation-wrapper {
    display: flex;
    flex-direction: row-reverse;
    padding: 12px 23px;
    background-color: #fff;

    .el-button {
      margin-left: 10px;
    }
  }
  .content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 15px;
    overflow: hidden;
  }
  .search-wrapper {
    padding: 15px;
    background-color: #fff;
  }
  .filter-item {
    .el-form-item {
      margin-bottom: 0;
      white-space: nowrap;
    }
    .el-input {
      width: auto;
      vertical-align: middle;
    }
    .el-button {
      margin-left: 15px;
      vertical-align: middle;
    }
  }
  .table-wrapper {
    flex: 1;
    background-color: #fff;

    .el-button {
      padding: 0;
    }
  }
  .pagination-wrapper {
    padding-top: 10px;
    .el-pagination {
      float: right;
      padding: 0;
      background-color: transparent;
    }
  }

</style>
<style lang="less">
</style>
